<template>
  <a-modal
    title="出厂放行"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <!-- 车牌号显示行（只读） -->
      <a-form-item label="车牌号">
        <div class="plate-number-display">
          <span class="plate-province">{{ record.province }}</span>
          <span class="plate-body">{{ record.plateBody }}</span>
        </div>
        <!-- 隐藏字段用于表单提交 -->
        <a-input
          v-decorator="['province']"
          type="hidden"
        />
        <a-input
          v-decorator="['plateBody']"
          type="hidden"
        />
      </a-form-item>

      <!-- 进厂时间（默认当前时间，可修改） -->
      <a-form-item label="出厂时间">
        <a-date-picker
          v-decorator="['outTime', {rules: [{required: true, message: '请选择出厂时间'}]}]"
          show-time
          format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%"
        />
      </a-form-item>

    </a-form>
  </a-modal>
</template>

<script>

import moment from 'moment'

export default {
  data() {
    return {
      visible: false,
      form: this.$form.createForm(this),
      record: {}
    }
  },
  methods: {
    show(record) {
      this.record = record
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue({
          province: record.province,
          plateBody: record.plateBody,
          // 设置默认值为当前时间
          outTime: record.outTime ? moment(record.outTime) : moment()
        })
      })
    },
    handleOk() {
      this.form.validateFields((err, values) => {
        if (!err) {
          // 格式化时间为字符串
          const formattedValues = {
            ...values,
            outTime: values.outTime.format('YYYY-MM-DD HH:mm:ss')
          }
          this.$emit('ok', {
            ...this.record,
            ...formattedValues
          })
          this.visible = false
        }
      })
    },
    handleCancel() {
      this.visible = false
    }
  }
}
</script>
<style scoped>
/* 车牌号显示样式 */
.plate-number-display {
  display: flex;
  align-items: center;
  height: 40px;
  padding: 0 15px;
  background: #f5f5f5;
  border-radius: 4px;
  font-size: 18px;
}

.plate-province {
  padding: 0 5px;
  background: #1890ff;
  color: white;
  border-radius: 2px;
  margin-right: 10px;
}

.plate-body {
  font-weight: bold;
  letter-spacing: 1px;
}
</style>